package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.PageInfo;
import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysRoleQueryDTO;
import cn.com.dcsgo.domain.dto.SysRoleSaveDTO;
import cn.com.dcsgo.domain.dto.SysRoleUpdateDTO;
import cn.com.dcsgo.domain.po.SysRole;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * SysRoleController
 *
 * @author Dcsgo
 * @since 1/6/2025 下午 2:23
 */
@Tag(name = "角色")
@RestController
@RequestMapping("sysRole")
@RequiredArgsConstructor
public class SysRoleController extends BaseController {
    private final SysRoleService sysRoleService;

    @MyPermission({"system:role:query"})
    @Operation(summary = "分页查询角色信息")
    @PostMapping("getByPage")
    public Result<PageInfo<SysRole, SysRoleQueryDTO>> getPage(@RequestBody PageInfo<SysRole, SysRoleQueryDTO> pageInfo) {
        PageInfo<SysRole, SysRoleQueryDTO> result = sysRoleService.page(pageInfo);
        return new Result<>(result);
    }

    @MyPermission({"system:role:query"})
    @Operation(summary = "获取角色信息", description = "根据角色编号查询角色信息")
    @GetMapping("get/{roleId}")
    public Result<SysRole> get(@PathVariable Long roleId) {
        SysRole sysRole = sysRoleService.get(roleId);
        return new Result<>(sysRole);
    }

    @MyPermission({"system:role:query"})
    @Operation(summary = "获取所有角色信息", description = "当前登录用户所拥有的所有角色")
    @GetMapping("getAllRoles")
    public Result<List<SysRole>> get() {
        List<SysRole> roleList = sysRoleService.getAll();
        return new Result<>(roleList);
    }

    @MyPermission({"system:role:query"})
    @Operation(summary = "根据用户id获取角色id")
    @GetMapping("getRoleIdsByUserId/{userId}")
    public Result<List<Long>> getRoleIds(@PathVariable Long userId) {
        List<Long> roleIds = sysRoleService.getRoleIds(userId);
        return new Result<>(roleIds);
    }

    @MyPermission({"system:user:selectRole"})
    @Operation(summary = "为用户分配角色")
    @PostMapping("saveUserRoles")
    public Result<Serializable> saveUserRoles(@RequestParam("userId") Long userId,
                                              @RequestParam("roleIds") List<Long> roleIds) {
        int row = sysRoleService.saveUserRoles(userId, roleIds);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:role:query"})
    @Operation(summary = "获取角色关联的菜单编号", description = "菜单编号只包含状态为正常且为叶子节点的")
    @GetMapping("getCurrentRoleHasMenuIdsByRoleId/{roleId}")
    public Result<List<Long>> getLeafMenuIds(@PathVariable Long roleId) {
        List<Long> leafMenuIdList = sysRoleService.getLeafMenuIds(roleId);
        return new Result<>(leafMenuIdList);
    }

    @MyPermission({"system:role:add"})
    @Operation(summary = "添加角色")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysRoleSaveDTO saveDTO) {
        int row = sysRoleService.save(saveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:role:selectPermission"})
    @Operation(summary = "给角色分配权限", description = "向角色菜单关联表中添加数据")
    @PostMapping("saveRoleMenu")
    public Result<Serializable> save(@RequestParam("roleId") Long roleId,
                                     @RequestParam("menuIds") List<Long> menuIds) {
        int row = sysRoleService.save(roleId, menuIds);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:role:update"})
    @Operation(summary = "修改角色")
    @PutMapping("update")
    public Result<Serializable> update(@Validated @RequestBody SysRoleUpdateDTO updateDTO) {
        int row = sysRoleService.update(updateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:role:remove"})
    @Operation(summary = "删除角色", description = "一并删除关联表中的数据")
    @DeleteMapping("delete/{roleId}")
    public Result<Serializable> update(@PathVariable Long roleId) {
        int row = sysRoleService.remove(roleId);
        return chooseReturn(row > 0);
    }
}
